10 //Mouse Control Script using IR
11 //by vkapadia with much assistance from inio
12 //vkapadia@vkapadia.com
15 //To calibrate, run this program and put the Wiimote on a flat surface face-up.
16 //Then read the values in the debug line (next to the run button).
17 //Change these values until the debug line reads approx. all zeros.
30 var.deadzone = 5 //distance in pixels that you have to move the wiimote in
31 //order for it to register movement. Creates a "dead zone" around the pointer
32 //to make it easier to click. Higher = smoother but less accurate.
35 //cursor2.visible = true
37 //more options to be added later
40 //Point Wiimote = Move Mouse
42 //B-Button = Left Click
44 //A-Button = Right Click
45 //Plus and Minus = Control Volume
49 //If the pointer hits the edge of the screen, the Wiimote will rumble a bit.
51 //The LEDs attempt to emulate KITT's grill from Knight Rider
54 //***Do not edit anything below this line unless you know what you are doing.***
55 var.accx1 = wiimote1.RawForceX + var.xtrim1
56 var.accy1 = wiimote1.RawForceY + var.ytrim1
57 var.accz1 = wiimote1.RawForceZ + var.ztrim1
59 if wiimote1.dot1vis and wiimote1.dot2vis then
61 if var.accy1 > -7 then
63 elseif var.accy1 > -45 then
73 if var.leftpoint1 = 0 then
74 if var.orientation1 = 0 then
75 if wiimote1.dot1x < wiimote1.dot2x then
81 if var.orientation1 = 1 then
82 if wiimote1.dot1y > wiimote1.dot2y then
88 if var.orientation1 = 2 then
89 if wiimote1.dot1x > wiimote1.dot2x then
95 if var.orientation = 3 then
96 if wiimote1.dot1y < wiimote1.dot2y then
104 if var.leftpoint1 = 1 then
105 var.fix1x1 = wiimote1.dot1x
106 var.fix1y1 = wiimote1.dot1y
107 var.fix2x1 = wiimote1.dot2x
108 var.fix2y1 = wiimote1.dot2y
110 var.fix1x1 = wiimote1.dot2x
111 var.fix1y1 = wiimote1.dot2y
112 var.fix2x1 = wiimote1.dot1x
113 var.fix2y1 = wiimote1.dot1y
116 var.dx1 = var.fix2x1 - var.fix1x1
117 var.dy1 = var.fix2y1 - var.fix1y1
118 var.cx1 = (var.fix1x1+var.fix2x1)/1024.0 - 1
119 var.cy1 = (var.fix1y1+var.fix2y1)/1024.0 - .75
121 var.d1 = sqrt(var.dx1*var.dx1+var.dy1*var.dy1)
123 var.dx1 = var.dx1 / var.d1
124 var.dy1 = var.dy1 / var.d1
126 var.ox1 = -var.dy1*var.cy1-var.dx1*var.cx1;
127 var.oy1 = -var.dx1*var.cy1+var.dy1*var.cx1;
129 var.ax1 = (var.ox1 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
130 var.ay1 = (-var.oy1* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
132 var.dx1 = var.ax1 - cursor2.posx
133 var.dy1 = var.ay1 - cursor2.posy
135 var.d1 = sqrt((var.dx1*var.dx1)+(var.dy1*var.dy1))
137 var.a1 = 180 / (200 + var.d1 * var.d1 * var.d1 * .001)
139 if var.d1 <= var.deadzone then var.a1 = 1
141 //debug = var.d + " " + var.a
143 var.finalx1 = cursor2.posx * var.a1 + var.ax1 * (1 - var.a1)
144 var.finaly1 = cursor2.posy * var.a1 + var.ay1 * (1 - var.a1)
147 cursor2.posx = smooth(var.finalx1,3,5)
148 cursor2.posy = smooth(var.finaly1,3,5)
156 var.xpos1 = var.finalx1
157 var.ypos1 = var.finaly1
158 ppjoy1.analog0 = ensuremaprange(var.xpos1,0,screen.desktopwidth* var.coeff,-1,1)
159 ppjoy1.analog1 = ensuremaprange(var.ypos1,0,screen.desktopheight* var.coeff,-1,1)
161 if wiimote1.B or wiimote1.A or wiimote1.Up or wiimote1.down or wiimote1.Left or wiimote1.Right == true
162 ppjoy1.digital0 = true
164 ppjoy1.digital0 = false
168 //***Do not edit anything below this line unless you know what you are doing.***
169 var.accx2 = wiimote2.RawForceX + var.xtrim2
170 var.accy2 = wiimote2.RawForceY + var.ytrim2
171 var.accz2 = wiimote2.RawForceZ + var.ztrim2
173 if wiimote2.dot1vis and wiimote2.dot2vis then
175 if var.accy2 > -7 then
177 elseif var.accy2 > -45 then
178 if var.accx2 < 0 then
187 if var.leftpoint2 = 0 then
188 if var.orientation2 = 0 then
189 if wiimote2.dot1x < wiimote2.dot2x then
195 if var.orientation2 = 1 then
196 if wiimote2.dot1y > wiimote2.dot2y then
202 if var.orientation2 = 2 then
203 if wiimote2.dot1x > wiimote2.dot2x then
209 if var.orientation = 3 then
210 if wiimote2.dot1y < wiimote2.dot2y then
218 if var.leftpoint2 = 1 then
219 var.fix1x2 = wiimote2.dot1x
220 var.fix1y2 = wiimote2.dot1y
221 var.fix2x2 = wiimote2.dot2x
222 var.fix2y2 = wiimote2.dot2y
224 var.fix1x2 = wiimote2.dot2x
225 var.fix1y2 = wiimote2.dot2y
226 var.fix2x2 = wiimote2.dot1x
227 var.fix2y2 = wiimote2.dot1y
230 var.dx2 = var.fix2x2 - var.fix1x2
231 var.dy2 = var.fix2y2 - var.fix1y2
232 var.cx2 = (var.fix1x2+var.fix2x2)/1024.0 - 1
233 var.cy2 = (var.fix1y2+var.fix2y2)/1024.0 - .75
235 var.d2 = sqrt(var.dx2*var.dx2+var.dy2*var.dy2)
237 var.dx2 = var.dx2 / var.d2
238 var.dy2 = var.dy2 / var.d2
240 var.ox2 = -var.dy2*var.cy2-var.dx2*var.cx2;
241 var.oy2 = -var.dx2*var.cy2+var.dy2*var.cx2;
243 var.ax2 = (var.ox2 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
244 var.ay2 = (-var.oy2* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)
246 var.dx2 = var.ax2 - cursor3.posx
247 var.dy2 = var.ay2 - cursor3.posy
249 var.d2 = sqrt((var.dx2*var.dx2)+(var.dy2*var.dy2))
251 var.a2 = 180 / (200 + var.d2 * var.d2 * var.d2 * .001)
253 if var.d2 <= var.deadzone then var.a2 = 1
255 //debug = var.d + " " + var.a
257 var.finalx2 = cursor3.posx * var.a2 + var.ax2 * (1 - var.a2)
258 var.finaly2 = cursor3.posy * var.a2 + var.ay2 * (1 - var.a2)
261 cursor3.posx = smooth(var.finalx2,3,5)
262 cursor3.posy = smooth(var.finaly2,3,5)
270 var.xpos2 = var.finalx2
271 var.ypos2 = var.finaly2
272 ppjoy2.analog0 = ensuremaprange(var.xpos2,0,screen.desktopwidth* var.coeff,-1,1)
273 ppjoy2.analog1 = ensuremaprange(var.ypos2,0,screen.desktopheight* var.coeff,-1,1)
275 if wiimote2.B or wiimote2.A or wiimote2.Up or wiimote2.down or wiimote2.Left or wiimote2.Right == true
276 ppjoy2.digital0 = true
278 ppjoy2.digital0 = false
281 debug = var.accx2+ " " + var.accy2+ " " + var.accz2